import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:im_app/im/dao/ImSessionDao.dart';
import 'package:im_app/im/page/bloc/SessionListCubit.dart';
import 'package:im_app/im/pojo/ImPojo.dart';
import 'package:im_app/im/util/DbUtil.dart';
import 'package:im_app/im/util/FirstLetterUtil.dart';
import 'package:logger/logger.dart';
import 'package:uuid/uuid.dart';

void main() {
  var _logger = Logger(
    printer: PrettyPrinter(),
  );

  test("uuid test", () async {
    var uuid = new Uuid();
    String u5 = uuid.v5(null, null);
    _logger.i("emoji content:  ${u5}");
  });

  test("firstLetter test", () async {
    String str1 = "asdfafas";
    String fl = FirstLetterUtil.firstLetter(str1);
    print("fl: $fl");

    str1 = "中国你好";
    fl = FirstLetterUtil.firstLetter(str1);
    print("fl: $fl");

    str1 = 'さんこんにちは';
    fl = FirstLetterUtil.firstLetter(str1);
    print("fl: $fl");
  });

  test('order test', () async {
    List<ImSession> imSessionList = [];
    ImSession session1 = createSession("test001", imSessionList);
    ImSession session2 = createSession("test002", imSessionList);
    ImSession session3 = createSession("test003", imSessionList);
    ImSession session4 = createSession("test004", imSessionList);
    session2.isTop = true; session2.isTop2 = true;
    session4.isTop = true; session4.isTop2 = true;

    var sessionDao = ImSessionDao();
    sessionDao.sort(imSessionList);
    // imSessionList.sort((s1, s2) {
    //   if (s1.isTop && s2.isTop) {
    //     return s2.updateTime!.compareTo(s1.updateTime!);
    //   }
    //   if (s1.isTop) return -1;
    //   if (s2.isTop) return 1;
    //   int ht = s2.hotMessageTotal!.compareTo(s1.hotMessageTotal!);
    //   int ut = s2.updateTime!.compareTo(s1.updateTime!);
    //   if (ht == 0) {
    //     return ut;
    //   }
    //   return ht;
    // });
    imSessionList.forEach((s){
      _logger.i("sessionName: ${s.name}");
    });
  });


}

ImSession createSession(String name, List<ImSession> sessions) {
  ImSession session1 = ImSession.buildWithUser("", buildUser(name));
  sleep(Duration(milliseconds: 100));
  session1.updateTime = DateTime.now();
  session1.toUserId = '0000';
  sessions.add(session1);
  return session1;
}

ImUser buildUser(String userName) {
  ImUser imUser = ImUser(userName);
  imUser.userName = userName;
  return imUser;
}